/**
 * @license
 * Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
 * SPDX-License-Identifier: Apache-2.0
 */
import Route from './Route.js';
import compose from 'koa-compose';
import RouteMap from './RouteMap.js';
import Context from './createContext.js';
export default class FakeServer {
    constructor({ routes, middleware = [], plugins = [] }) {
        this.middleware = middleware;
        Object.entries(routes).forEach(([prefix, callback]) => {
            new Route(prefix, callback, this.RouteMap, {});
        });
        this._initPlugins(plugins);
    }
    default = {
        response: {},
    };
    RouteMap = new RouteMap();
    getResult = async (request) => {
        const result = this.RouteMap.matchRoute(request.method.toUpperCase(), request.url);

        if (typeof result === 'function') {
            const ctx = new Context({ request, response: this.default.response });
            await compose([...this.middleware, result])(ctx);
            return ctx.response;
        } else {
            console.log('没有匹配到');
        }
    };
    _initPlugins(plugins) {
        plugins.forEach((plugin) => plugin.call(this, this));
    }
}
